/*	
File:		Claw.h
Author:		Ben Feldmann
Purpose:	Class for managing the claw weapon
*/

#include "Claw.h"
#include "ObjectManager.h"
#include "GamePlayState.h"
#include "EventSystem.h"
#include "Game.h"

#include "SGD Wrappers/CSGD_TextureManager.h"
#include "SGD Wrappers/CSGD_XAudio2.h"


Claw::Claw(Player* owner)
{
	isAttacking	 = false;
	isSpcAttacking = false;
	spcAttacked	 = false;
	returning    = false;
	isAttacking = false;

	atkTimer	= 0.0f;
	spcAtkTimer = 0.0f;
	rotation	= 0.0f;

	grabbed = false;
	enemy = nullptr;

	SetTextureID(CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/claw.png")));

	SetOwner(owner);
	SetDamage(25.0f);
	SetSpcDamage(30.0f);
	SetCoolTimer(10.0f);
	SetCoolSeconds(10.0f); // Test Variable
	SetAtkSpeed(.5f);
	SetEnergyCost(15.0f);
	SetMetalPrice(75);
	SetCircuitPrice(75);
	SetX(GetOwner()->GetX() + (GetOwner()->GetWidth()/2.0f));
	SetY(GetOwner()->GetY() + (GetOwner()->GetHeight()/2.0f));
	SetHeight(5);
	SetWidth(5);

	meleeType = MW_CLAW;
	type = OBJ_WEAPON;

	ObjectManager::GetInstance()->AddObject(this);
	Release();

	//clawAttack = CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("Sound/Melee Weapons/clawAttack.wav"));
}

Claw::~Claw()
{
	if( enemy )
		enemy->Release();

	if(GetTextureID() != -1 )
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(GetTextureID());
		SetTextureID(-1);
	}
	//if(clawAttack != -1)
	//{
	//	CSGD_XAudio2::GetInstance()->SFXStopSound(clawAttack);
	//	CSGD_XAudio2::GetInstance()->SFXUnloadSound(clawAttack);
	//	clawAttack = -1;
	//}

	ObjectManager::GetInstance()->RemoveObject(this);
}

bool Claw::CheckCollision(IEntity* base) 
{
	if(Entity::CheckCollision(base) == true && base != this->GetOwner() && base->GetType() != OBJ_PICKUP && base->GetType() != OBJ_WEAPON)
	{
		/*if(base->GetType() == OBJ_TILE && dynamic_cast<Tile*>(base)->GetSolid() == true)
		returning = true;*/

		if(base->GetType() == OBJ_TILE)
			return false;

		if(base->GetType() == OBJ_CHEST && spcAttacked == true)
			returning = true;

		if(GetOwner()->GetClock() == false && base->GetType() == OBJ_ENEMY && grabbed == false)
		{
			Enemy* tmp = dynamic_cast<Enemy*>(base);
			if(isAttacking == true)
			{
				EventSystem::GetInstance()->SendEvent("target_hit", base, new float(GetDamage()));
				tmp->SetSlow(1.0f, 1.0f);
				return true;
			}
			else if(isSpcAttacking == true)
			{
				EventSystem::GetInstance()->SendEvent("target_hit", base, new float(GetSpcDamage()));
				enemy = (Enemy*)base;
				enemy->AddRef();
				grabbed = true;
				returning = true;

				CSGD_XAudio2::GetInstance()->SFXPlaySound(GetOwner()->GetClawSound());

				return true;
			}
			else
				return false;
		}
		else if (GetOwner()->GetClock() == true && base->GetType() == OBJ_ENEMY)
		{
			Enemy* tmp = dynamic_cast<Enemy*>(base);
			if(isAttacking == true)
			{
				EventSystem::GetInstance()->SendEvent("target_hit", base, new float(GetDamage()*1.5f));
				tmp->SetSlow(1.0f, 1.0f);
				return true;
			}
			else if(isSpcAttacking == true)
			{
				EventSystem::GetInstance()->SendEvent("target_hit", base, new float(GetSpcDamage()*1.5f));
				enemy = (Enemy*)base;
				enemy->AddRef();
				grabbed = true;
				returning = true;

				CSGD_XAudio2::GetInstance()->SFXPlaySound(GetOwner()->GetClawSound());

				return true;
			}
			else
				return false;
		}
		else 
			return false;
	}
	else
		return false;
}

void Claw::Attack() 
{

	if(isAttacking == false)
	{
		weaponAnimation[GetDirection()].Play();
		isAttacking = true;

		/*switch(GetDirection())
		{
		case 3:
			SetY(GetY()-32);
			break;

		case 0:
			SetX(GetX()+32);
			break;

		case 2:
			SetY(GetY()+32);
			break;

		case 1:
			SetX(GetX()-32);
			break;
		}*/

		//CSGD_XAudio2::GetInstance()->SFXPlaySound(clawAttack);
	}
}

void Claw::SpecialAttack() 
{
	if(isSpcAttacking == false && spcAttacked == false)
	{
		weaponAnimation[GetDirection()].Play();
		isSpcAttacking = true;
		spcAttacked  = true;
		GetOwner()->SetEnergy(GetOwner()->GetEnergy() - GetEnergyCost());

		SetCoolTimer(0.0f);
	}
}

void Claw::Update(float elapsed)
{
	if(isAttacking == false && isSpcAttacking == false && returning == false)
	{
		SetDirection(GetOwner()->GetDirection());
		SetX(GetOwner()->GetX() + (GetOwner()->GetWidth()/2.0f) + 8);
		SetY(GetOwner()->GetY() + (GetOwner()->GetHeight()/2.0f) + 8);
	}

	if(GetOwner()->GetAnimation() != &weaponAnimation[GetDirection()])
	{
		GetOwner()->SetAnimation(&weaponAnimation[GetDirection()]);
	}

	if(isAttacking == true)
	{
		atkTimer += elapsed;

		if(atkTimer >= GetAtkSpeed())
		{
			isAttacking = false;
			atkTimer = 0.0f;
		}
	}
	if(spcAttacked == true)
	{
		if(isSpcAttacking == true)
		{
			spcAtkTimer += elapsed;

			switch(GetDirection())
			{
			case 3:
				SetY(GetY() - (300*elapsed));
				break;

			case 0:
				SetX(GetX() + (300*elapsed));
				break;

			case 2:
				SetY(GetY() + (300*elapsed));
				break;

			case 1:
				SetX(GetX() - (300*elapsed));
				break;
			}
		}
		else
			SetCoolTimer(GetCoolTimer() + elapsed);

		if(returning == true || grabbed == true)
		{
			if(sqrt(((GetX()-GetOwner()->GetX())*(GetX()-GetOwner()->GetX())) + ((GetY()-GetOwner()->GetY())*(GetY()-GetOwner()->GetY()))) < 60)
			{
				returning = false;
				grabbed = false;
			}

			int prevX = (int)GetX(); 
			int prevY = (int)GetY();

			if(grabbed == false && GetX() < GamePlayState::GetInstance()->GetCamX()+Game::GetInstance()->GetScreenWidth() && GetY() < GamePlayState::GetInstance()->GetCamY()+Game::GetInstance()->GetScreenHeight()
				&& GetX() > GamePlayState::GetInstance()->GetCamX() && GetY() > GamePlayState::GetInstance()->GetCamY())
			{
				//if(GetX() > GetOwner()->GetX())
				//	SetX(GetX()-(400*elapsed));
				//else if(GetX() < GetOwner()->GetX())
				//	SetX(GetX()+(400*elapsed));

				//if(GetY() > GetOwner()->GetY())
				//	SetY(GetY()-(400*elapsed));
				//else if(GetY() < GetOwner()->GetY())
				//	SetY(GetY()+(400*elapsed));

				if(GetX() < GetOwner()->GetX()-50)
				{
					SetX(GetX()+(400*elapsed));
				}
				else if(GetX() > GetOwner()->GetX()+50)
				{
					SetX(GetX()-(400*elapsed));
				}
				if(GetY() < GetOwner()->GetY()-50)
				{
					SetY(GetY()+(400*elapsed));
				}
				else if(GetY() > GetOwner()->GetY()+50)
				{
					SetY(GetY()-(400*elapsed));
				}


			}
			else if(grabbed == true && GetX() < GamePlayState::GetInstance()->GetCamX()+Game::GetInstance()->GetScreenWidth() && GetY() < GamePlayState::GetInstance()->GetCamY()+Game::GetInstance()->GetScreenHeight()
				&& GetX() > GamePlayState::GetInstance()->GetCamX() && GetY() > GamePlayState::GetInstance()->GetCamY())
			{
				//if(GetX() > GetOwner()->GetX())
				//{
				//	SetX(GetX()-(400*elapsed));
				//	enemy->SetX((GetX()+GetWidth())-(400*elapsed));
				//}
				//else if(GetX() < GetOwner()->GetX())
				//{
				//	SetX(GetX()+(400*elapsed));
				//	enemy->SetX((GetX()-GetWidth())-(400*elapsed));
				//}

				//if(GetY() > GetOwner()->GetY())
				//{
				//	SetY(GetY()-(400*elapsed));
				//	enemy->SetY((GetY()+GetHeight())-(400*elapsed));
				//}
				//else if(GetY() < GetOwner()->GetY())
				//{
				//	SetY(GetY()+(400*elapsed));
				//	enemy->SetY((GetY()-GetHeight())-(400*elapsed));

				//}

				if(GetX() < GetOwner()->GetX()-50)
				{
					SetX(GetX()+(400*elapsed));
					enemy->SetX(GetX()+(400*elapsed));
				}
				else if(GetX() > GetOwner()->GetX()+50)
				{
					SetX(GetX()-(400*elapsed));
					enemy->SetX(GetX()-(400*elapsed));
				}
				if(GetY() < GetOwner()->GetY()-50)
				{
					SetY(GetY()+(400*elapsed));
					enemy->SetY(GetY()+(400*elapsed));
				}
				else if(GetY() > GetOwner()->GetY()+50)
				{
					SetY(GetY()-(400*elapsed));
					enemy->SetY(GetY()-(400*elapsed));
				}
			}

			if(prevX == GetX() && prevY == GetY())
				returning = false;
		}

		if(spcAtkTimer >= 1.0f)
		{
			isSpcAttacking = false;
			spcAtkTimer = 0.0f;
			returning = true;
		}

		if(GetCoolTimer() >= GetCoolSeconds())
		{
			spcAttacked = false;
			//SetCoolTimer(0.0f);
			returning = false;
			grabbed = false;
		}
	}
	if( !grabbed && enemy )
	{
		enemy->Release();
		enemy = nullptr;
	}
}

void Claw::Render()
{
	CSGD_Direct3D::GetInstance()->GetSprite()->Flush();

	if(GetOwner()->GetCurWeapon() == 1)
	{
		if(spcAttacked == true)
		{
			CSGD_TextureManager::GetInstance()->Draw(GetTextureID(), int(GetX() - GamePlayState::GetInstance()->GetCamX()), 
				int(GetY() - GamePlayState::GetInstance()->GetCamY()),1.0f, 1.0f,nullptr,0.0f,0.0f,rotation);
		}

		/*RECT rect = GetRect();
		CSGD_Direct3D::GetInstance()->GetSprite()->Flush();
		CSGD_Direct3D::GetInstance()->DrawRect(rect,255,255,255);*/
	}
}

RECT Claw::GetRect()
{
	RECT rect = {};
	if(spcAttacked == true)
	{
		rect.top	= (LONG)GetY();
		rect.bottom = (LONG)GetY()+GetHeight();
		rect.left	= (LONG)GetX();
		rect.right	= (LONG)GetX()+GetWidth();

		return rect;
	}
	else
	{
		int dir = GetDirection();

		switch(dir)
		{
		case 0:
			{
				rect = MeleeWeapon::GetRect();
				rect.right += 10;
			}
			break;
		case 1:
			{
				rect = MeleeWeapon::GetRect();
				rect.left -= 10;
			}
			break;
		case 2:
			{
				rect = MeleeWeapon::GetRect();
			}
			break;
		case 3:
			{
				rect = MeleeWeapon::GetRect();
			}
			break;
		default:
			{
			}
			break;
		}
		return rect;
	}
}
